<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录注册</title>
    <base href="/">

    <link rel="stylesheet" href="layui/css/layui.css">
    <style>
        /* 全局样式 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        /* 容器样式 */
        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 350px;
        }

        /* 标题样式 */
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        /* 输入框样式 */
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        /* 按钮样式 */
        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 切换按钮样式 */
        .toggle-btn {
            text-align: center;
            margin-top: 15px;
        }

        .toggle-btn a {
            color: #007BFF;
            text-decoration: none;
        }

        .toggle-btn a:hover {
            text-decoration: underline;
        }

        /* 错误提示样式 */
        .error {
            color: red;
            margin-bottom: 10px;
            text-align: center;
        }
    </style>
</head>

<body>
<div class="container" id="login-container">
    <h2>管理员登录</h2>
    <div class="error" id="login-error"></div>
    <input type="text" id="login-username" placeholder="用户名">
    <input type="password" id="login-password" placeholder="密码">
    <button onclick="login()">登录</button>
    <div class="toggle-btn">
        <a href="javascript:;" onclick="showRegistration()">没有账号？注册</a>
    </div>
</div>

<div class="container" id="registration-container" style="display: none;">
    <h2>管理员注册</h2>
    <div class="error" id="registration-error"></div>
    <input type="text" id="reg-username" placeholder="用户名">
    <input type="password" id="reg-password" placeholder="密码">
    <input type="password" id="reg-confirm-password" placeholder="确认密码">
    <button onclick="register()">注册</button>
    <div class="toggle-btn">
        <a href="javascript:;" onclick="showLogin()">已有账号？登录</a>
    </div>
</div>

<script src="layui/layui.all.js"></script>
<script>
    const $ = layui.$;
    function showRegistration() {
        document.getElementById('login-container').style.display = 'none';
        document.getElementById('registration-container').style.display = 'block';
    }

    function showLogin() {
        document.getElementById('registration-container').style.display = 'none';
        document.getElementById('login-container').style.display = 'block';
    }

    function login() {
        const username = document.getElementById('login-username').value;
        const password = document.getElementById('login-password').value;

        if (username === '' || password === '') {
            document.getElementById('login-error').innerHTML = '用户名和密码不能为空';
            return;
        }

        let reg = /^[a-zA-Z0-9]{6,16}$/;
        if (reg.test(username) && reg.test(password)) {
            // 登录操作
            $.ajax({
                url: 'system/dologin',
                data: { username: username, password: password },
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("登录成功", { icon: 1 });
                        setTimeout(function () {
                            location.href = "system/index";
                        }, 1800);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        } else {
            layer.msg("用户名和密码必须是 6 - 16 位字符数字");
            return;
        }

    }

    function register() {
        const username = document.getElementById('reg-username').value;
        const password = document.getElementById('reg-password').value;
        const confirmPassword = document.getElementById('reg-confirm-password').value;

        if (username === '' || password === '' || confirmPassword === '') {
            document.getElementById('registration-error').innerHTML = '所有字段不能为空';
            return;
        }

        if (password !== confirmPassword) {
            document.getElementById('registration-error').innerHTML = '两次输入的密码不一致';
            return;
        }

        let reg = /^[a-zA-Z0-9]{6,16}$/;
        if (reg.test(username) && reg.test(password)) {
            $.ajax({
                url: 'system/doregister',
                data: { username: username, password: password},
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("注册成功", { icon: 1 });
                        setTimeout(function () {
                            location.href = "system/login";
                        }, 1800);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        } else {
            layer.msg("用户名和密码必须是 6 - 16 位字符数字");
            return;
        }
    }
</script>
</body>

</html>